/*
 *  Input.cpp
 *  NxtFace
 *
 *  Created by Dave on 10/3/06.
 *  Copyright 2006 __MyCompanyName__. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "SBJ/NXT/Input.h"
#include "SBJ/NXT/Command.h"

namespace SBJ
{

namespace NXT
{

// Constructors
Input::Input( Device& device, InputPort port )
: m_device( device )
{
	m_snapshot.port = port;
	get();
}
	
Input::~Input( void )
{
}
	
// Accessors
InputPort
Input::getPort( void ) const
{
	return m_snapshot.port;
}

SensorType
Input::getType( bool refresh )
{
	if ( refresh ) get();
	return m_snapshot.type;
}

SensorMode
Input::getMode( bool refresh )
{
	if ( refresh ) get();
	return m_snapshot.mode;
}
 
bool
Input::isCalibrated( bool refresh )
{
	if ( refresh ) get();
	return m_snapshot.calibrated;
}

UBytes2
Input::getRawValue( bool refresh )
{
	if ( refresh ) get();
	return m_snapshot.rawValue;
}

UBytes2
Input::getNormalizedValue( bool refresh )
{
	if ( refresh ) get();
	return m_snapshot.normValue;
}

SBytes2
Input::getScaledValue( bool refresh )
{
	if ( refresh ) get();
	return m_snapshot.scaledValue;
}

SBytes2
Input::getCalibratedValue( bool refresh )
{
	if ( refresh ) get();
	return m_snapshot.calibratedValue;
}

// Manipulators
void
Input::setType( SensorType type, bool query, Synchonous syncType )
{
	if ( query ) get();
	m_snapshot.type = type;
	set( syncType );
}

void
Input::setMode( SensorMode mode, bool query, Synchonous syncType )
{
	if ( query ) get();
	m_snapshot.mode = mode;
	set( syncType );
}

void
Input::reset( void )
{
	Command< ResetInputScaledValue > command( m_device, m_snapshot.port );
}

void
Input::set( Synchonous syncType )
{
	Command< SetInput >( m_device, m_snapshot, syncType );
}

void
Input::get( void )
{
	Command< GetInput > download( m_device, GetInput::Params( m_snapshot.port ) );
	if ( download.result().valid ) m_snapshot = download.result();
}

}

}